package com.speech.utils;

import android.content.Context;

import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;

import java.util.Calendar;

public class PropertyTodayDataHolder {

    private static Integer iToday_ReadCount = 0;
    private static Double dToday_CoinReceive = 0.0d;
    private static Long lTodayTime = -1L;

    private static Double dToday_PropertyBegin = 0d;

    public static Integer getiToday_ReadCount() {
        return iToday_ReadCount;
    }

    public static Double getdToday_CoinReceive() {
        return dToday_CoinReceive;
    }

    public static void init(Context context, Long lCurTime) {
        boolean bChangeDay = _checkDayChange(context,lCurTime);
        if (bChangeDay) {
            _reset(context, lCurTime);
        } else {
            SharedPreferenceHelper spf = new SharedPreferenceHelper(context, PrefsConstant.FILE_TODAY_DATA);
            Long lTime = (Long) spf.getData(PrefsConstant.KEY_TODAY_DATA_LONG_TIME, SharedPreferenceHelper.LONG_TYPE_DEFAULT);
            if (lTime != SharedPreferenceHelper.LONG_TYPE_DEFAULT) {
                lTodayTime = lTime;
            } else {
                lTodayTime = lCurTime;
                spf.putData(PrefsConstant.KEY_TODAY_DATA_LONG_TIME, lTodayTime);
            }

            Integer iSentenceCount = (Integer) spf.getData(PrefsConstant.KEY_TODAY_DATA_INT_SENTENCE_COUNT, SharedPreferenceHelper.INT_TYPE_DEFAULT);
            if (iSentenceCount != SharedPreferenceHelper.INT_TYPE_DEFAULT) {
                iToday_ReadCount = iSentenceCount;
            } else {
                iToday_ReadCount = 0;
                spf.putData(PrefsConstant.KEY_TODAY_DATA_INT_SENTENCE_COUNT, 0);
            }

            String strCoinReceive = (String) spf.getData(PrefsConstant.KEY_TODAY_DATA_STRING_COIN_RECEIVE, SharedPreferenceHelper.STR_TYPE_DEFAULT);
            if (!strCoinReceive.equals(SharedPreferenceHelper.STR_TYPE_DEFAULT)) {
                dToday_CoinReceive = Double.parseDouble(strCoinReceive);
            } else {
                dToday_CoinReceive = 0.0d;
                spf.putData(PrefsConstant.KEY_TODAY_DATA_STRING_COIN_RECEIVE, "0.0");
            }
        }
    }

    private static void _reset(Context context, Long lTime) {
        lTodayTime = lTime;
        dToday_CoinReceive = 0.0d;
        iToday_ReadCount = 0;

        SharedPreferenceHelper spf = new SharedPreferenceHelper(context, PrefsConstant.FILE_TODAY_DATA);
        spf.putData(PrefsConstant.KEY_TODAY_DATA_STRING_COIN_RECEIVE, dToday_CoinReceive);
        spf.putData(PrefsConstant.KEY_TODAY_DATA_INT_SENTENCE_COUNT, iToday_ReadCount);
        spf.putData(PrefsConstant.KEY_TODAY_DATA_LONG_TIME, lTodayTime);

    }

    private static boolean _checkDayChange(Context context,Long ltime) {

        SharedPreferenceHelper spf = new SharedPreferenceHelper(context, PrefsConstant.FILE_TODAY_DATA);
        Long lTime = (Long) spf.getData(PrefsConstant.KEY_TODAY_DATA_LONG_TIME, SharedPreferenceHelper.LONG_TYPE_DEFAULT);
        if (lTime != SharedPreferenceHelper.LONG_TYPE_DEFAULT) {
            lTodayTime = lTime;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(lTodayTime);
        int iRecordDate = calendar.get(Calendar.DATE);

        calendar.setTimeInMillis(ltime);
        int iCurrentTime = calendar.get(Calendar.DATE);

        if (iCurrentTime - iRecordDate != 0) {
            return true;
        }

        return false;
    }

    public static void increase(Context context,double add_coin, Long lCurrentTime) {
        if (_checkDayChange(context,lCurrentTime)) {
            _reset(context,lCurrentTime);
        }

        iToday_ReadCount += 1;
        dToday_CoinReceive += add_coin;

        SharedPreferenceHelper spf = new SharedPreferenceHelper(context, PrefsConstant.FILE_TODAY_DATA);
        spf.putData(PrefsConstant.KEY_TODAY_DATA_STRING_COIN_RECEIVE, dToday_CoinReceive+"");
        spf.putData(PrefsConstant.KEY_TODAY_DATA_INT_SENTENCE_COUNT, iToday_ReadCount);

    }

}
